package com.ifgi.sitcom.campusmapperNDrawer.fragments;

import java.util.ArrayList;










import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabWidget;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.gamificationlibrary.fragment.SpinnerLeaderboardsFragment;
import com.gamificationlibrary.fragment.RadioLeaderboardsFragment;
import com.gamificationlibrary.model.Player;
import com.gamificationlibrary.model.leaderboard.Leaderboard;
import com.gamificationlibrary.view.LeaderboardView;
import com.ifgi.sitcom.campusmapperNDrawer.R;
import com.ifgi.sitcom.campusmapperNDrawer.controllers.CampusMapperApplication;
import com.ifgi.sitcom.campusmapperNDrawer.controllers.PlayerController;


public class LeaderboardFragment extends Fragment{
	
	private TabHost tHost;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View rootView = inflater.inflate(R.layout.tab, container, false);
		Bundle bundle=getArguments();
		ArrayList<Leaderboard> leaderboards=(ArrayList<Leaderboard>) bundle.get(LeaderboardView.EXTRAS_LEADERBOARD);
	    final ArrayList<Leaderboard> l1=new  ArrayList<Leaderboard>();
	    l1.add(leaderboards.get(0));
	    l1.add(leaderboards.get(1));
	    l1.add(leaderboards.get(2));
	    final ArrayList<Leaderboard> l2=new  ArrayList<Leaderboard>();
	    l2.add(leaderboards.get(3));
	    l2.add(leaderboards.get(4));
	    final String pID=CampusMapperApplication.getInstance().getPlayer().getIdentification();
		
	    tHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
	    tHost.setup();
	    
	    /** Defining Tab Change Listener event. This is invoked when tab is changed */
	    TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {
	
	        @Override
	        public void onTabChanged(String tabId) {
	        	
	        	 android.support.v4.app.FragmentManager fm =    getActivity().getSupportFragmentManager();
	           
	        	SpinnerLeaderboardsFragment geoFragment = (SpinnerLeaderboardsFragment) fm.findFragmentByTag("geo");
	            RadioLeaderboardsFragment temporalFragment = (RadioLeaderboardsFragment) fm.findFragmentByTag("temporal");
	            android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
	
	            /** Detaches the androidfragment if exists */
	            if(temporalFragment!=null)
	                ft.detach(temporalFragment);
	
	            /** Detaches the applefragment if exists */
	            if(geoFragment!=null)
	                ft.detach(geoFragment);
	
	            /** If current tab is android */
	            if(tabId.equalsIgnoreCase("temporal")){
	
	                if(temporalFragment==null){
	                    /** Create AndroidFragment and adding to fragmenttransaction */
	                	RadioLeaderboardsFragment radioFragement=new RadioLeaderboardsFragment();

	    				Bundle b=new Bundle();
	    				b.putSerializable(LeaderboardView.EXTRAS_LEADERBOARD, l1);
	    				b.putString(LeaderboardView.EXTRAS_PLAYER, pID);
	    				radioFragement.setArguments(b);
	    				radioFragement.setFieldsRadioButton(new String[]{"2Weekly","Monthly","All time"});
	                	
	                	ft.add(R.id.realtabcontent,radioFragement, "temporal");
	                	
	                }else{
	                    /** Bring to the front, if already exists in the fragmenttransaction */
	                    ft.attach(temporalFragment);
	                }
	
	            }else{    /** If current tab is apple */
	                if(geoFragment==null){
	                    /** Create AppleFragment and adding to fragmenttransaction */
	                	SpinnerLeaderboardsFragment spinnerFragment=new SpinnerLeaderboardsFragment();
	                	
	                	Bundle b=new Bundle();
	    				b.putSerializable(LeaderboardView.EXTRAS_LEADERBOARD, l2);
	    				b.putString(LeaderboardView.EXTRAS_PLAYER, pID);
	    				spinnerFragment.setArguments(b);
	    				spinnerFragment.setFieldsSpinner(new String[]{CampusMapperApplication.getInstance().getArea(1).getName()
	                			,CampusMapperApplication.getInstance().getArea(2).getName()});
	    				spinnerFragment.setColorTextSpinner(CampusMapperApplication.getInstance().getArea_color());
	                    ft.add(R.id.realtabcontent,spinnerFragment, "geo");
	                 }else{
	                    /** Bring to the front, if already exists in the fragmenttransaction */
	                    ft.attach(geoFragment);
	                }
	            }
	           
	            ft.commit();
	        }
	    };
	
	    /** Setting tabchangelistener for the tab */
	    tHost.setOnTabChangedListener(tabChangeListener);
	
	    /** Defining tab builder for Andriod tab */
	    TabHost.TabSpec tSpecTemporal = tHost.newTabSpec("temporal");
	    tSpecTemporal.setIndicator("Total scores");
	    tSpecTemporal.setContent(new DummyTabContent(getActivity().getBaseContext()));
	    tHost.addTab(tSpecTemporal);
	
	    /** Defining tab builder for Apple tab */
	    TabHost.TabSpec tSpecGeo = tHost.newTabSpec("geo");
	    tSpecGeo.setIndicator("Geo scores");
	    tSpecGeo.setContent(new DummyTabContent(getActivity().getBaseContext()));
	    tHost.addTab(tSpecGeo);
	    TabWidget widget = tHost.getTabWidget();
	    for(int i = 0; i < widget.getChildCount(); i++) {
	        View v = widget.getChildAt(i);
	        v.setBackgroundResource(R.drawable.action_bar_tab_bg);
	    }
	    ((SherlockFragmentActivity) getActivity()).getSupportActionBar().removeAllTabs();
	    ((SherlockFragmentActivity) getActivity()).getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);  
		
	    return rootView;
	}
	
	
	
	public class DummyTabContent implements TabContentFactory{
	    private Context mContext;
	 
	    public DummyTabContent(Context context){
	        mContext = context;
	    }
	 
	    @Override
	    public View createTabContent(String tag) {
	        View v = new View(mContext);
	        return v;
	    }
	}
}
